var str = "";

function initTable() {
    ajaxCall("GET",path+"/ModelServlet?method=getTable",setTable)
}
function setTable(res) {
    var obj=JSON.parse(res);
    var table=document.getElementById("t1");
    clearTable("t1");
    for(var i in obj){
        var newRow=table.insertRow(1);
        var id=newRow.insertCell(0);
        var content=newRow.insertCell(1);
        var whenClick=newRow.insertCell(2);
        id.innerHTML=obj[i].id;
        content.innerHTML=obj[i].content;
        whenClick.innerHTML="<input type='button' value='选择' onclick='details(this)'/>"
    }
}
function details(row) {
    var rowIndex=row.parentNode.parentNode.rowIndex;
    var table=document.getElementById("t1");
    var id = table.rows[rowIndex].cells[0].innerHTML;
    var content=table.rows[rowIndex].cells[1].innerHTML;

    var table=document.getElementById("tques");
    var newRow=table.insertRow(1);
    var id1 = newRow.insertCell(0);
    var content1=newRow.insertCell(1);
    var whenClick=newRow.insertCell(2);
    id1.innerHTML = id;
    content1.innerHTML=content;
    whenClick.innerHTML="<input type='button' value='删除' onclick='DelRowNew(this)' />"
}

function clearTable(tableName) {
    var tb = document.getElementById(tableName);
    var rowNum = tb.rows.length;
    for (i = 1; i < rowNum; i++) {
        tb.deleteRow(i);
        rowNum = rowNum - 1;
        i = i - 1;
    }
}



function insertTo() {
    var content =  document.getElementById("tquestion").value;
    var question={content:""};
    question.content=content;
    if(content==""){
        alert("内容为空");
    }
    else {
        ajaxCall( "POST", path+"/ModelServlet?method=insertTo",setT,question)
    }
}
function setT(res) {
    var obj=JSON.parse(res);
    ClearTextArea();
    var table=document.getElementById("tques");
    var newRow=table.insertRow(1);
    var id = newRow.insertCell(0);
    var content=newRow.insertCell(1);
    var whenClick=newRow.insertCell(2);
    id.innerHTML = obj.id;
    content.innerHTML=obj.content;
    whenClick.innerHTML="<input type='button' value='删除' onclick='DelRowNew(this)' />"
}
function DelRowNew(r){
    var i=r.parentNode.parentNode.rowIndex;
    document.getElementById('tques').deleteRow(i);
}
function ClearTextArea()
{
    document.getElementById("tquestion").value="";
}
